﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json.Converters;
using System.Web.Script.Serialization;
using System.IO;

namespace Siva.CBC.Barnacle.ashx
{
    public class JsonSerialize
    {
        public static string Serialize(Object obj)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                //DefaultValueHandling = DefaultValueHandling.Ignore
            };

            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            //jsSerializer.MaxJsonLength = Int32.MaxValue;
            //return jsSerializer.Serialize(obj);
            return JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented, settings);

        }

        public static T Deserialize<T>(string json)
        {
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            return jsSerializer.Deserialize<T>(json);
        }
    }

    public class DateTimeFormatConverter:IsoDateTimeConverter
    {
        public DateTimeFormatConverter(string format)
        {
            DateTimeFormat = format;
        }
    }
}